package com.example.flowabledemo.demos.flow.controller;

import com.example.flowabledemo.demos.flow.component.FlowComponent;
import com.example.flowabledemo.demos.flow.component.SkipNodeComponent;
import com.example.flowabledemo.demos.flow.dto.VariablesDTO;
import com.example.flowabledemo.demos.flow.entity.ProcessFormDetail;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 流程测试
 * @Author: LuanKay
 * @Date: 2025/2/13
 */
@Slf4j
@RestController
@AllArgsConstructor
public class FlowableController {
    private final FlowComponent flowComponent;

    private final SkipNodeComponent skipNodeComponent;

    /**
     * 部署流程
     */
    @PostMapping("/deploy")
    public void deploy() {
        // 读取bpmn文件流
        InputStream bpmnStream = getClass().getResourceAsStream("/process/请假流程4.bpmn20.xml");
        log.info("deploy接口返回:{}", flowComponent.deploy(bpmnStream));
    }

    /**
     * 发起流程
     *
     * @param deployId 流程部署ID
     */
    @PostMapping("/start")
    public void start(@RequestParam("deployId") String deployId, @RequestParam("name") String name) {
        log.info("start接口返回:{}", flowComponent.startProcess(deployId, name));
    }


    /**
     * 提交任务
     *
     * @param paramDTO 任务参数DTO
     */
    @PostMapping("/complete")
    public void complete(@RequestBody VariablesDTO paramDTO) {
        log.info("提交参数如下:{}", paramDTO.toString());
        Map<String, Object> variables = new HashMap<>();
        //form转map
        paramDTO.getValues().forEach(value -> variables.put(value.getKey(), value.getValue()));
        flowComponent.completeTask(paramDTO.getProcessId(), paramDTO.getUser(), variables);
    }


    /**
     * 查看流程当前未完成节点
     *
     * @param processId 流程实例ID
     */
    @GetMapping("/unfinished/{processId}")
    public void unfinished(@PathVariable("processId") String processId) {
        flowComponent.getUnfinishedNodes(processId);
    }

    /**
     * 获取流程实例图base64
     *
     * @param processId 流程实例ID
     */
    @GetMapping("/image/{processId}")
    public String image(@PathVariable("processId") String processId) throws IOException {
        String imageBase64 = flowComponent.getProcessImage(processId);
        log.info("流程图base64:{}", imageBase64);
        return imageBase64;
    }

    /**
     * 获取当前节点表单
     *
     * @param processId 流程实例ID
     */
    @GetMapping("/form/{processId}")
    public List<ProcessFormDetail> form(@PathVariable("processId") String processId) {
        return flowComponent.getFormByProcessId(processId);
    }

    /**
     * 获取所有激活未完成的流程
     */
    @GetMapping("/unfinishedProcess")
    public void unfinishedProcess() {
        flowComponent.getUnfinishedProcess();
    }

    /**
     * 获取所有历史用户节点
     *
     * @param processId 流程实例ID
     */
    @GetMapping("/getHistoryUserNode/{processId}")
    public void getHistoryUserNode(@PathVariable("processId") String processId) {
        flowComponent.getHistoryUserNode(processId);
    }

    /**
     * 跳转至指定节点
     *
     * @param processId 流程实例ID
     * @param taskKey   节点key
     */
    @PostMapping("/skip")
    public void skip(@RequestParam("processId") String processId, @RequestParam("taskKey") String taskKey) {
        skipNodeComponent.skipNode(processId, taskKey);
    }


}
